<?php
namespace app\demo\controller;


use think\Controller;
use think\Db;
use app\demo\model\User;
use think\Validate;
use think\Loader;

class Index extends Controller
{
    //视图输出
    public function index()
    {
        $this->assign('word','Hello,word!');
        $this->assign('user',Db::name('user')->select());

        //方法1
        return $this->fetch();
//        方法2
//        return view();
    }


    //数据库查询
    public function table(){

//        table()内的数据表名要写完整

//        方法1 使用model查询

//        $user = model('User');
//        $user = $user->where('id','neq','2')->select();

////            查询某个字段值
//        $name = $user->where('id',1)->value('name');
//
////            查询某一列的值
//        $name = $user->where('utime','neq','')->column('name');
//        //使用id直接查询
//        $user = User::get(2);//文件头部需添加 use app\demo\model\User;
//
//        $user = new User; //文件头部需添加 use app\demo\model\User;
//        $user = $user->where('id','neq','2')->select();
//
//
//        //方法2  单例查询（类似M方法）
////        若存在表前缀think_
//        $user = Db::table('think_user')->where('name&remark','like','%thinkphp')->find();
//        $user = Db::name('user')->select();
//
//
//        //方法3  使用助手函数  每次都进数据库查询
//        $user = db('user')->select();
//
//        //方法4
//        $user = new \think\db\Query();
//        $user->table('user')->where('id','neq','');
//        Db::find($user);
//        $user = Db::select($user);
//
//        //方法5 闭包查询
//        $user = new \think\db\Query();
//        $user = Db::select(function($user){
//            $user->table('user')->where('utime','');
//        });

//        $id = $this->request->post('id');
//        $group = Db::name('group')->select();
//       return json($group);
        $this->view->engine->layout(false);
        $group = Db::name('group')->select();
        $this->assign('group',$group);
        return $this->fetch();
    }

    public function index_second(){
        $this->view->engine->layout(false);
        return $this->fetch();
    }

    public function change_group(){
        $id = $this->request->param('id');
//        $group = Db::name('group')->alias('a')->field('a.*')
//            ->join('group_user b','b.group_id=a.id')->where('b.user_id',$id)->find();
        $group_id = Db::name('group_user')->where('user_id',$id)->value('group_id');
        $data = Db::name('user')->alias('a')->field('a.*,b.group_id as group_id')
            ->join('group_user b','b.user_id = a.id')->where('b.group_id',$group_id)->select();

        return json($data);
    }

    //数据添加
    public function insert(){
        if(request()->isPost()){
            $arr = $this->request->post('arr/a');
            print_r($arr);
            //           获取url参数变量
            $key = input('key');
            $key = $this->request->param('key');
            echo $key;

            //获取表单某个变量（不同形式）
            $name = $this->request->param('name');
            $name = $this->request->post('name');
            $name = $this->request->get('name');

            //获取表单所有变量
            $all = $this->request->param();
            $all = $this->request->post();
            $all = $this->request->get();

            print_r($all) ;

//            添加记录
            $data = [
                'name' => 'bb',
                'pwd'=>md5('123456'),
                'remark' => '123123',
                'utime'=> time()
            ];
            $user = Db::name('user')->insert($data);
            //获取插入记录的id
            $insertId = Db::name('user')->insertGetId($data);
            echo $insertId;

            //插入多条记录
            $data = [
                ['name' => '越前龙马','pwd'=>md5('123456'), 'remark' => '123123','utime'=> time()],
                ['name' => '蜡笔小新','pwd'=>md5('123456'), 'remark' => '123123','utime'=> time()],
                ['name' => '张三丰','pwd'=>md5('123456'), 'remark' => '123123','utime'=> time()]
            ];
            Db::name('user')->insertAll($data);


            //自动验证
            //头部添加 use think\Validate;

            // 使用User验证器类 在当前模块下新建view/validate/User.php
            $data = $this->request->post();
            $result = $this->validate($data,'User.add');
            if(true !== $result){
                $this->error($result);
            }else{
                print_r($data);
            }

//            控制器内直接验证
            $validate = new Validate(
            [
                'name'  => 'require|max:3',
                'pwd'   => 'require|max:6',
                'remark'   => 'email',
            ],
            [
                'name.require' => '名称必须',
                'name.max'     => '名称最多不能超过3个字符',
                'pwd.require'   => '密码不为空',
                'remark'        => '邮箱格式错误',
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
               $this->error($validate->getError());
            }else{
                print_r($data);
            }
        }else{
            return  $this->fetch();
        }

    }

    //数据更新
    public function edit()
    {
        $data['remark'] = 'test_update';
        $data['utime'] = time();

        $user = new User;
        $update = $user->where('id',2)->update($data);
    }

    public function delete(){

        //方法1
        $user = User::get(22);
        $user->delete();

//        方法2
        $del = Db::name('user')->delete(22);
        $dele = Db::name('user')->delete([22,23,24]);
    }


}
